//
//  geoNotesViewController.m
//  geoNotes
//
//  Created by Administrator on 6/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "geoNotesViewController.h"
#import "NoteDB.h"
#import "NoteDetailsView.h"
@implementation geoNotesViewController
@synthesize noteTableView;
@synthesize db;

- (void)dealloc
{
    [noteTableView release];
    [super dealloc];
}
-(void)viewDidLoad{
     [super viewDidLoad];
    db=[[NoteDB alloc] init];
   // [self addFakeDataToDB];
  //  [self saveDB];
    [self loadDB];
}
-(void)awakeFromNib{
    
}

-(void) addFakeDataToDB{
    [db addNoteToDB:[[[Note alloc] initWithTitle:@"Hello World1" andMessage:@"Test message 1" withLatitude:[NSNumber numberWithDouble:48.303156] andLongitude:[NSNumber numberWithDouble:14.291656]] autorelease]];
    [db addNoteToDB:[[[Note alloc] initWithTitle:@"Hello World2" andMessage:@"Test message 2" withLatitude:[NSNumber numberWithDouble:48.303446] andLongitude:[NSNumber numberWithDouble:14.290526]] autorelease]];
    [db addNoteToDB:[[[Note alloc] initWithTitle:@"Hello World3" andMessage:@"Test message 3" withLatitude:[NSNumber numberWithDouble:48.303566] andLongitude:[NSNumber numberWithDouble:14.291456]] autorelease]];
    [db addNoteToDB:[[[Note alloc] initWithTitle:@"Hello World4" andMessage:@"Test message 4" withLatitude:[NSNumber numberWithDouble:48.303676] andLongitude:[NSNumber numberWithDouble:14.290516]] autorelease]];
    [db addNoteToDB:[[[Note alloc] initWithTitle:@"Hello World5" andMessage:@"Test message 5" withLatitude:[NSNumber numberWithDouble:48.303756] andLongitude:[NSNumber numberWithDouble:14.291586]] autorelease]];
    [db addNoteToDB:[[[Note alloc] initWithTitle:@"Hello World6" andMessage:@"Test message 6" withLatitude:[NSNumber numberWithDouble:48.303896] andLongitude:[NSNumber numberWithDouble:14.290596]] autorelease]];
   
  //  NSLog(@"fake :%@",[db description]);
    
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [db notesInDB];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell.
    cell.textLabel.text = [db noteAtPosition:indexPath.row].title;
    
    return cell;
}
- (void)tableView: (UITableView *) tableView
didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
    /*
    NSLog(@"row: %d",indexPath.row);
    NSLog(@"elements in db: %d",[db notesInDB]);
    Note* selected=[db noteAtPosition:indexPath.row];
    NSLog(@"retain : %d",[selected retainCount]);
    NSLog(@"retain : %d",[selected.title retainCount]);
    NSLog(@"retain : %d",[selected.message retainCount]);
   NSLog(@"%@",selected.title);
    NSLog(@"%@",selected.message);
     */
    NoteDetailsView*controller = [[NoteDetailsView alloc] initWithNibName:@"NoteDetailsView" bundle:nil];
    controller.delegate = self;
    controller.note=[db noteAtPosition:indexPath.row];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    
    [controller release];
}
-(void) noteDetailsViewDidFinish{
    [self dismissModalViewControllerAnimated:YES];
}
-(void)noteDetailsViewDidFinish:(UIViewController *)controller withDelete:(Note *)note{
    [db deleteNote:note];
    [noteTableView reloadData];
    [self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
    [self saveDB];
    [self setNoteTableView:nil];
    db=nil;
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)onAddNote:(id)sender {
    AddNoteView*controller = [[AddNoteView alloc] initWithNibName:@"AddNoteView" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];

}

- (IBAction)onMapPress:(id)sender {
    NoteMapView*controller = [[NoteMapView alloc] initWithNibName:@"NoteMapView" bundle:nil];
    controller.delegate = self;
    controller.db=[[db database] retain];
    controller.notedb=db;
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];
}
-(void) addNoteViewDidFinish:(UIViewController*)controller withTitle:(NSString*)title andMessage:(NSString*)message{
   
    [db addNoteToDB:[[[Note alloc] initWithTitle:title andMessage:message withLatitude:[NSNumber numberWithDouble:48.303056] andLongitude:[NSNumber numberWithDouble:14.290556]] autorelease]];
    [noteTableView reloadData];
    [self dismissModalViewControllerAnimated:YES];
    
}
-(void) addNoteViewDidFinishWithCancel{
    [self dismissModalViewControllerAnimated:YES];
}
-(void) noteMapViewDidFinish{
    [noteTableView reloadData];
     [self dismissModalViewControllerAnimated:YES];
}
-(NSString *)docPath{
   
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return  [[[documentsDirectory stringByAppendingPathComponent: [NSString stringWithString:@"NoteDB.plist"]] copy] autorelease]; 
}
-(void)loadDB{
    db=[[NSKeyedUnarchiver unarchiveObjectWithFile:[self docPath]]retain];
    NSLog(@"loaded %d entries from plist ",[db notesInDB]);
    //NSLog(@"temp retaincount: %d",[temp retainCount]);
  
}
-(void)saveDB{
    NSLog(@"%@",@"saving db...");
    if([NSKeyedArchiver archiveRootObject:db toFile:[self docPath]]){
        NSLog(@"%@",@"db was saved!");
    }
    else{
         NSLog(@"%@",@"db couldn't be saved!");
    }

}
@end
